package com.elephant.core.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author hai
 * @description
 * @date 13:38 2018/8/30
 */
public class MySocketClient {

    private static final Integer COUNT = 10;

    public static void main(String[] args) {

//        Socket socket = getSocket();
        Socket socket = getSocketByInetAddress();
        OutputStream outputStream;
        try {
            outputStream = socket.getOutputStream();
            for (int i = 0; i < COUNT ; i++) {

                String temp = "中国"+ i + "\n";
                outputStream.write(temp.getBytes());
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    
    public static Socket getSocketByInetAddress(){
        InetAddress inetAddress;
        Socket socket = null;
        try {
            inetAddress = InetAddress.getLocalHost();
            socket = new Socket(inetAddress,9099);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return socket;
    }


    public static Socket getSocket(){
        Socket socket = null;
        try {
            socket = new Socket("localhost",9099);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket;
    }
}
